package ws.performancetest.tests.jdbcdriver;

import ws.performancetest.tests.jdbcdriver.runnable.cjdbc.CJDBCDeleteRunnable;
import ws.performancetest.tests.jdbcdriver.runnable.cjdbc.CJDBCInsertRunnable;
import ws.performancetest.tests.jdbcdriver.runnable.cjdbc.CJDBCUpdateRunnable;
import ws.performancetest.tests.jdbcdriver.runnable.cjdbc.CJDBCReadRunnable;
import ws.performancetest.tests.jdbcdriver.runnable.jdbc.JDBCDeleteRunnable;
import ws.performancetest.tests.jdbcdriver.runnable.wjdbc.WJDBCDeleteRunnable;
import ws.performancetest.tests.jdbcdriver.runnable.wjdbc.WJDBCInsertRunnable;
import ws.performancetest.tests.jdbcdriver.runnable.jdbc.JDBCInsertRunnable;
import ws.performancetest.tests.jdbcdriver.runnable.wjdbc.WJDBCUpdateRunnable;
import ws.performancetest.tests.jdbcdriver.runnable.jdbc.JDBCUpdateRunnable;
import ws.performancetest.tests.jdbcdriver.runnable.wjdbc.WJDBCReadRunnable;
import ws.performancetest.tests.jdbcdriver.runnable.ResultSetRunnable;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
import java.sql.Connection;
import java.sql.SQLException;
import ws.jdbc.concurrent.driver.sqlserver.CDriver;
import ws.performancetest.api.annotation.PerformanceTest;
import ws.performancetest.api.exception.TestExecutionException;
import ws.performancetest.api.exception.TestSuiteSetupException;
import ws.performancetest.tests.jdbcdriver.runnable.jdbc.JDBCReadRunnable;
import static java.sql.ResultSet.*;

/**
 *
 * @author Wilson Santos, 36228 <wilson.santos@ua.pt>
 */
public class JDBCDriverTest extends AbstractDriverTest {

  private SQLServerDriver msdriver = new SQLServerDriver();
  private CDriver cdriver = new CDriver();

  public JDBCDriverTest() throws TestSuiteSetupException, SQLException {
    super("JDBCDriverTest_Results", "JDBCDriverTest_Summary");
  }

  // <editor-fold defaultstate="collapsed" desc="Microsoft's driver tests">
  // <editor-fold defaultstate="collapsed" desc="READ">
  @PerformanceTest(name = "JDBC Leitura")
  public void testMSDriverSR() throws SQLException, TestExecutionException {
    final Connection tmpcon = msdriver.connect(dburl, null);

    executeReadOrUpdateTest(JDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupJDBC(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "WJDBC Leitura")
  public void testMSDriverSRCursorWrapper() throws SQLException, TestExecutionException {
    final Connection tmpcon = msdriver.connect(dburl, null);

    executeReadOrUpdateTest(WJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupWJDBC(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }
  // </editor-fold>

  // <editor-fold defaultstate="collapsed" desc="UPDATE">
  @PerformanceTest(name = "JDBC Actualização")
  public void testMSDriverSU() throws SQLException, TestExecutionException {
    final Connection tmpcon = msdriver.connect(dburl, null);

    executeReadOrUpdateTest(JDBCUpdateRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupJDBC(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "WJDBC Actualização")
  public void testMSDriverSUCursorWrapper() throws SQLException, TestExecutionException {
    final Connection tmpcon = msdriver.connect(dburl, null);

    executeReadOrUpdateTest(WJDBCUpdateRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupWJDBC(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }
  // </editor-fold>

  // <editor-fold defaultstate="collapsed" desc="INSERT">
  @PerformanceTest(name = "JDBC Inserção")
  public void testMSDriverSI() throws SQLException, TestExecutionException {
    final Connection tmpcon = msdriver.connect(dburl, null);

    executeInsertTest(JDBCInsertRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupJDBC(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "WJDBC Inserção")
  public void testMSDriverCursorWrapperSI() throws SQLException, TestExecutionException {
    final Connection tmpcon = msdriver.connect(dburl, null);

    executeInsertTest(WJDBCInsertRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupWJDBC(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }
  // </editor-fold>

  // <editor-fold defaultstate="collapsed" desc="DELETE">
  @PerformanceTest(name = "JDBC Remoção")
  public void testMSDriverSD() throws SQLException, TestExecutionException {
    final Connection tmpcon = msdriver.connect(dburl, null);

    executeDeleteTest(JDBCDeleteRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupJDBC(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "WJDBC Remoção")
  public void testMSDriverCursorWrapperSD() throws SQLException, TestExecutionException {
    final Connection tmpcon = msdriver.connect(dburl, null);

    executeDeleteTest(WJDBCDeleteRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupWJDBC(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }
  // </editor-fold>
  // </editor-fold>

  // <editor-fold defaultstate="collapsed" desc="CDriver tests">
  // <editor-fold defaultstate="collapsed" desc="READ">
  @PerformanceTest(name = "CJDBC_I Leitura")
  public void testCDriverSRIndividualCache() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCI(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_S Leitura")
  public void testCDriverSharedCacheRead() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCReadRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCS(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_READ_ONLY,
                cycleCount);
      }
    });

    tmpcon.close();
  }
  // </editor-fold>

  // <editor-fold defaultstate="collapsed" desc="UPDATE">
  @PerformanceTest(name = "CJDBC_I Actualização")
  public void testCDriverIndividualCacheSU() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCUpdateRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCI(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_S Actualização")
  public void testCDriverSharedCacheSU() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeReadOrUpdateTest(CJDBCUpdateRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCS(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount);
      }
    });

    tmpcon.close();
  }
  // </editor-fold>

  // <editor-fold defaultstate="collapsed" desc="INSERT">
  @PerformanceTest(name = "CJDBC_I Inserção")
  public void testCDriverIndividualCacheSI() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeInsertTest(CJDBCInsertRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCI(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_S Inserção")
  public void testCDriverSharedCacheSI() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeInsertTest(CJDBCInsertRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCS(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount);
      }
    });

    tmpcon.close();
  }
  // </editor-fold>

  // <editor-fold defaultstate="collapsed" desc="DELETE">
  @PerformanceTest(name = "CJDBC_I Remoção")
  public void testCDriverIndividualCacheSD() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeDeleteTest(CJDBCDeleteRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCI(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount * numberOfThreads, cycleCount);
      }
    });

    tmpcon.close();
  }

  @PerformanceTest(name = "CJDBC_S Remoção")
  public void testCDriverSharedCacheSD() throws SQLException, TestExecutionException {
    final Connection tmpcon = cdriver.connect(dburl, null);

    executeDeleteTest(CJDBCDeleteRunnable.class, new ISetup() {

      @Override
      public void setup() throws SQLException {
        ResultSetRunnable.setupCJDBCS(tmpcon, sqlQuery,
                TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE,
                cycleCount);
      }
    });

    tmpcon.close();
  }
  // </editor-fold>
  // </editor-fold>
}
